package com.own.business.log.operation.persistence.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.own.business.log.operation.persistence.business.service.LogOperationService;
import com.own.business.log.operation.persistence.entity.bo.LogOperationBo;
import com.own.business.log.operation.persistence.entity.map.LogOperationMapVo;
import com.own.business.log.operation.persistence.entity.po.LogOperation;
import com.own.business.log.operation.persistence.entity.query.LogOperationQuery;
import com.own.business.log.operation.persistence.entity.vo.LogOperationVo;
import com.own.business.log.operation.persistence.mapper.LogOperationMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * LogOperationServiceImpl
 *
 * @author chenxueli
 * @date 2023-08-03 09:29:59
 */
@Slf4j
@Service
public class LogOperationServiceImpl extends AbstractBaseService<
        LogOperation,
        LogOperationBo,
        LogOperationVo,
        LogOperationMapVo,
        LogOperationQuery,
        LogOperationMapper
        > implements LogOperationService {

    @Resource
    private LogOperationMapper logOperationMapper;

    /**
     * 获取查询条件
     *
     * @param query 查询条件
     * @return 查询条件对象
     */
    @Override
    public LambdaQueryWrapper<LogOperation> lambdaQueryWrapper(LogOperationQuery query) {
        return super.lambdaQueryWrapper(query)
                .eq(query.getLevel() != null, LogOperation::getLevel, query.getLevel())
                .eq(query.getUserId() != null, LogOperation::getFkUserId, query.getUserId())
                .eq(StringUtils.isNotBlank(query.getToken()), LogOperation::getToken, query.getToken())
                .likeRight(StringUtils.isNotBlank(query.getMethodName()), LogOperation::getMethodName, query.getMethodName())
                .eq(query.getStatus() != null, LogOperation::getStatus, query.getStatus())
                .like(StringUtils.isNotBlank(query.getUserName()), LogOperation::getUserName, query.getUserName())
                .like(StringUtils.isNotBlank(query.getClient()), LogOperation::getClient, query.getClient())
                .like(StringUtils.isNotBlank(query.getIp()), LogOperation::getIp, query.getIp())
                .like(StringUtils.isNotBlank(query.getModuleName()), LogOperation::getModuleName, query.getModuleName())
                .like(StringUtils.isNotBlank(query.getOperationName()), LogOperation::getOperationName, query.getOperationName())
                .likeRight(StringUtils.isNotBlank(query.getTraceId()), LogOperation::getTraceId, query.getTraceId())
                .orderByDesc(LogOperation::getGmtCreate);
    }

    /**
     * 根据链路id获取日志信息
     *
     * @param traceId 链路id
     * @return 日志信息
     */
    @Override
    public LogOperationVo getByTraceId(String traceId) {
        var wrapper = lambdaQueryWrapper().eq(LogOperation::getTraceId, traceId);
        var po = getOneByWrapper(wrapper);
        return afterGetById(ofVo(po));
    }

}
